﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI;
using OpenQuarters.WebQuarters.Core;
using OpenQuarters.WebQuarters.Utils;
using System.Web.UI.WebControls;

namespace OpenQuarters.WebQuarters.Modules.Redirect
{
    public class RedirectModule : Module
    {
        public RedirectModule()
            : base()
        {
        }

        public enum RedirectType
        {
            Url,
            FirstChild
        }

        public RedirectType redirect_Type = RedirectType.Url;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown)]
        public RedirectType Redirect_Type
        {
            get { return redirect_Type; }
            set { redirect_Type = value; }
        }
        
        public string redirect_Url = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string Redirect_Url
        {
            get { return redirect_Url; }
            set { redirect_Url = value; }
        }

        public override void Initialise()
        {
            this.ModuleName = "Redirect Module";
            this.ModuleDescription = "Redirects a page to a predefined location on load";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            string redirectUrl = null;
            string adminText = null;

            switch (this.redirect_Type)
            {
                case RedirectType.FirstChild:
                    if (CMSPage.Current.CMSPage.ChildPages != null && CMSPage.Current.CMSPage.ChildPages.Count != 0)
                    {
                        redirectUrl = "/site/" + CMSPage.Current.CMSPage.ChildPages.OrderBy(u => u.Order).FirstOrDefault().Urls.FirstOrDefault().PageUrl;
                        adminText = string.Format("The LIVE view of this page will redirect to (first child page): <a href=\"{0}\">{0}</a>", redirectUrl);
                    }
                    else
                    {
                        adminText = "The module is set to redirect to the 1st child page, but one does not exist";
                    }
                    break;
                default:
                    redirectUrl = this.redirect_Url;
                    adminText = string.Format("The LIVE view of this page will redirect to: <a href=\"{0}\">{0}</a>", this.redirect_Url);
                    break;
            }

            if (OpenQuarters.WebQuarters.Core.User.Current.HasPermission(Permissions.CMSPage.Edit, CMSPage.Current.PageId.ToString()))
            {
                this.Controls.AddLiteral(adminText);
            }
            else if (!string.IsNullOrEmpty(redirectUrl))
            {
                //((ContentPlaceHolder)this.Page.FindControl("HeadContent")).Controls.AddLiteral("<meta http-equiv=\"refresh\" content=\"0,url=" + redirectUrl + "\"  />");
                Oracle.Response.Redirect(redirectUrl, true);
                //this.Controls.AddLiteral("<script type=\"text/javascript\">document.location = '" + redirectUrl + "';</script>");
            }

        }
    }
}
